﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ComplexNumbers
{
    class ComplexNumber
    {
        private double realPart;
        private double imaginaryPart;

        public ComplexNumber(double realPart, double imaginaryPart)
        {
            this.realPart = realPart;
            this.imaginaryPart = imaginaryPart;
        }

        public double RealPart
        {
            get
            {
                return realPart;
            }
            set
            {
                realPart = value;
            }
        }

        public double ImaginaryPart
        {
            get
            {
                return imaginaryPart;
            }
            set
            {
                imaginaryPart = value;
            }
        }

        public static ComplexNumber operator +(ComplexNumber first, ComplexNumber second)
        {
            ComplexNumber result = new ComplexNumber(first.RealPart + second.RealPart, first.ImaginaryPart + second.ImaginaryPart);

            return result;
        }

        public static ComplexNumber operator -(ComplexNumber first, ComplexNumber second)
        {
            ComplexNumber result = new ComplexNumber(first.RealPart - second.RealPart, first.ImaginaryPart - second.ImaginaryPart);

            return result;
        }

        public static ComplexNumber operator *(ComplexNumber first, ComplexNumber second)
        {
            double resultReal = first.RealPart * second.RealPart - first.ImaginaryPart * second.ImaginaryPart;
            double resultImaginary = first.ImaginaryPart * second.RealPart + first.RealPart * second.ImaginaryPart;

            return new ComplexNumber(resultReal, resultImaginary);
        }

        public static ComplexNumber operator /(ComplexNumber first, ComplexNumber second)
        {
            double resultReal = (first.RealPart * second.RealPart + first.ImaginaryPart * second.ImaginaryPart) /
               ( second.RealPart * second.RealPart + second.ImaginaryPart * second.ImaginaryPart);

            double resultImaginary = (first.ImaginaryPart * second.RealPart - first.RealPart * second.ImaginaryPart) /
                (second.RealPart * second.RealPart + second.ImaginaryPart * second.ImaginaryPart);

            return new ComplexNumber(resultReal, resultImaginary);
        }

        public static explicit operator double(ComplexNumber c1)
        {
            return c1.RealPart;
        }

       

        public override string ToString()
        {
            return string.Format("{0} + {1}i", this.RealPart, this.ImaginaryPart);
        }

        public double this[int index]
        {
            get
            {
                if (index >= 0 && index <= 1)
                {
                    if (index == 0)
                    {
                        return this.RealPart;
                    }
                    else
                    {
                        return this.ImaginaryPart;
                    }
                }

                throw new IndexOutOfRangeException("Index must be in bounds [0,1]");
            }

        }


    }
}
